package com.test.nkbookshop.dao.homework;


import cn.edu.nankai.bookstore.dao.ReceiverDao;
import cn.edu.nankai.bookstore.dao.impi.GenericBaseDao;
import cn.edu.nankai.bookstore.domain.po.Receiver;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReceiverDaoImpl extends GenericBaseDao implements ReceiverDao {

    @Override
    public int insert(Receiver receiver) {
        int res = 0;
        String sql = "insert into receiver values(null,?)";
        try {
            this.getConnection();
            executeInsert(sql, receiver.getName());
            res = result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }


    @Override
    public int delete(Receiver receiver) {
        return delete(receiver.getReceiverId());
    }

    @Override
    public Receiver findById(Integer id) {
        Receiver receiver = null;
        String sql = "select * from receiver where receiver_id = ?";
        try {
            this.getConnection();
            this.executeQuery(sql, id);
            if (rs != null && rs.next()) {
                receiver = new Receiver(rs.getInt("receiver_id"), rs.getString("name"));
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return receiver;

    }

    @Override
    public ArrayList<Receiver> findAll() {
        ArrayList<Receiver> receivers = null;
        String sql = "select * from receiver";
        try {
            this.getConnection();
            this.executeQuery(sql);
            if (rs != null) {
                receivers = new ArrayList<>();
                while (rs.next()) {
                    Receiver receiver = new Receiver(rs.getInt("receiver_id"), rs.getString("name"));
                    receivers.add(receiver);
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return receivers;
    }

    @Override
    public int update(Receiver receiver) {
        int res = 0;
        String sql = "update receiver set name = ? where receiver_id = ? ";
        try {
            this.getConnection();
            this.executeUpdate(sql, receiver.getName(), receiver.getReceiverId());
            res = result;
            this.closeAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        String sql = "delete from receiver where receiver_id = ?";
        try {
            this.getConnection();
            this.executeUpdate(sql, id);
            res = result;
            this.closeAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public List<Receiver> findBySql(String sql, Object... params) {
        return null;
    }

}